﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace CourseRecommend
{
    public partial class SwapForm : Form
    {
        public SwapForm()
        {
            InitializeComponent();
        }

        private void SwapForm_Load(object sender, EventArgs e)
        {
            foreach (string item in RecommendMe.classNames)
            {
                swapBox.Items.Add(item);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
            HomeForm newhome = new HomeForm();
            newhome.Show();
            newhome.Activate();
        }

        private void SwapMeButton_Click(object sender, EventArgs e)
        {
            string classToSwapIn ="";
            
            try
            {
                classToSwapIn = swapBox.Text;
            }
            catch (Exception noSelection)
            {
                MessageBox.Show("Select a class or cancel");
            }
            SqlConnection myConnection = new SqlConnection(RecommendMe.connectionString);

            try
            {
                myConnection.Open();
            }
            catch (Exception p)
            {
                MessageBox.Show(e.ToString());
            }

            SqlCommand command = new SqlCommand("SELECT classId FROM [CatalogTable] WHERE Name =  @NAME", myConnection);
            command.Parameters.AddWithValue("@NAME", classToSwapIn);
            SqlDataReader reader = null;
            reader = command.ExecuteReader();
            //loop through all records 
            bool moreResults = true;
            string classID = "";
            
            while (moreResults)
            {
                while (reader.Read())
                {
                    classID = reader["ClassId"].ToString();
                    moreResults = reader.NextResult();
                }
            }
            reader.Close();
            myConnection.Close();


            if (!RecommendMe.tempHistoryList.Contains(classID))
            {
                if (RecommendMe.havePrereq(classID))
                {
                    RecommendMe.tempHistoryList.Add(classID);
                    HomeForm.tempRecommendedList.Add(classToSwapIn);
                    RecommendMe.recommendSchedule();
                }
                else MessageBox.Show("You do not have the prerequisites for the selected class");
            }
            else MessageBox.Show("You have already taken this class");
            // check to see if prereqs are met for the chosen class
            // If the are then send everything before the class we are swapping out and send it to a temp history list
            // using the temp history rerecommend the rest of the semesters
            this.Close();
            
            HomeForm newhome = new HomeForm();
            HomeForm.hasSwapped = true;
            newhome.Show();
            newhome.Activate();
        }
    }
}
